package com.nineclock.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * 统一鉴权
 * <p>
 * 安全框架默认密码加密
 * {noop} , 是不加密配置
 * <p>
 * EnableGlobalMethodSecurity: 允许使用注解的方式控制权限
 * prePostEnable : 允许使用@PreAuthorize, @PostAuthorize
 */
@Configuration
@EnableWebSecurity //开启web认证
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /*@Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(User.withUsername("admin")
                .password("{noop}123456")
                .authorities("p1", "ROLE_ADMIN")
                .build());
        inMemoryUserDetailsManager.createUser(User.withUsername("test")
                .password("{noop}123456")
                .authorities("o1", "test").build());
        return inMemoryUserDetailsManager;
    }*/






    /**
     * 密码123456
     * BCrypt加密
     * @return
     */
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }


    /*

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();

        inMemoryUserDetailsManager.createUser(User.withUsername("admin")
                .password("$2a$10$c2sZT/LtM1ExWfZjO0yIPeTGSqMSlX7oi.SvliMbeZpT9Y4qIBDue").authorities("p1", "ROLE_ADMIN").build());
        inMemoryUserDetailsManager.createUser(User.withUsername("test").password("$2a$10$c2sZT/LtM1ExWfZjO0yIPeTGSqMSlX7oi.SvliMbeZpT9Y4qIBDue").authorities("o1", "test").build());
        return inMemoryUserDetailsManager;
    }*/


    /**
     * csrf: 跨站请求伪造
     * csrf().disable(): 关闭跨站请求伪造
     * <p>
     * permitAll: 放行（不需要认证）
     * hasRole: 是否拥有某个角色
     * <p>
     * antMatchers("hello").hasRole("ADMIN"):  hello请求必须是 ADMIN 角色
     * antMatchers("say").hasAuthority("O1"); say 请求必须 有 O1 权限'
     * anyRequest().authenticated()  其他的请求，只要认证了就可以访问
     *
     * @param http
     * @throws Exception
     */

    /**
     * 与controller类注解交互
     * @param http
     * @throws Exception
     */
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .and()
                .csrf().disable()
                .logout().and().authorizeRequests()
                .antMatchers("/register").permitAll() //注册页面放行
//                .antMatchers("/hello").hasRole("ADMIN")
//                .antMatchers("/say").hasAuthority("O1")
                .anyRequest().authenticated();
    }

}
